package models;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import play.db.jpa.Model;
import utils.YUtils;
import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;
/**
 * 
 * @author masz 2012-11-13
 * 基建工程月报表
 */
@Entity
public class ConstructionProject extends Model{

	public String projectName;//项目名称
	
	public int capacity;//变电容量  单位MVA
	public double lineLength;//线路长度 km
	public double countInvest;//总投资  万元
	
	public double totalInvest;//累计投资  万元
	public int itemTotal;//项目数
	
	public double totalAmount;//本月完成投资 总额
	public double civil;//土建
	public double equipment;//设备
	public double erect;//安装
	public double line;//线路
	public double other;//其他
	
	public int 	qualityAccident;//质量事故次数
	public int securityAccident;//安全事故次数
	
	public String remark;
	
	@OneToMany(mappedBy = "consProject")
	public List<ProjectVisualizeReport> pvReprots;
	
	@ManyToOne
	public Dept belongDept;//所属分公司
	public String belongDate;//所属月份,格式：201211
	
	@ManyToOne
	public User deptVerifyUser;//分公司审核人
	public Date deptVerifyDate;//分公司审核时间
	
	@ManyToOne
	public User locationVerifyUser;//本地审核人
	public Date locationVerifyDate;//本地审核时间
	
	public int verifyState=0;//-2本地审核未通过，-1分公司审核未通过，0 未审核,1 分公司审核通过,2本地审核通过
	
	@ManyToOne
	public User noteUser;//填写人
	@ManyToOne
	public User modifyUser;//最后一次修改人
	public Date modifyDate;//最后一次修改时间
	
	public Date createDate=new Date();//创建日期
	
	@Transient
	public String pvrQuanlity;//工程形象 本月完成String，显示时使用
	
	@Transient
	public String pvrTotal;//工程形象建设累计完成String,显示时使用
	
	@Transient
	public String pvrJsonInfo;  //工程形象报表 json格式，修改是使用
	
	@Transient
	public String text;
	
	@Transient
	public long consProjectId;
	
	@Transient
	public String verifyStateStr;	//审核状态字符串
	
	public String getVerifyStateStr(){
		if(verifyState==-2){
			return "地电本部审核未通过";
		}
		if(verifyState==-1){
			return "地电分公司审核未通过";
		}
		if(verifyState==0){
			return "未审核";
		}
		if(verifyState==1){
			return "地电分公司审核通过";
		}
		if(verifyState==2){
			return "地电本部审核通过";
		}
		return "";
	}
	
	public String getPvrJsonInfo(){
		if(pvReprots!=null &&pvReprots.size()>0){
			String strJson = ProjectVisualizeReport.toSimpleJson(pvReprots);
			String json = YUtils.ligerGridData(strJson, pvReprots.size());
			return json ;
		}
		return "";
	}

	public String getText(){
		return projectName+"-"+belongDate;
	}
	
	public long getConsProjectId(){
		return id;
	}
	
	public String getPvrQuanlity(){
		String pvrQuanlity="";
		for(int i=0;i<pvReprots.size();i++){
			ProjectVisualizeReport pvr =  pvReprots.get(i);
			if(i==pvReprots.size()-1){
				pvrQuanlity+=pvr.projectVisuzlize.descriptions+"("+pvr.quanlity+")"+pvr.projectVisuzlize.unitQuantity;
			}else{
				pvrQuanlity+=pvr.projectVisuzlize.descriptions+"("+pvr.quanlity+")"+pvr.projectVisuzlize.unitQuantity+"<br/>";
			}
		}
		return pvrQuanlity;
	}
	public String getPvrTotal(){
		String pvrTote="";
		for(int i=0;i<pvReprots.size();i++){
			ProjectVisualizeReport pvr =  pvReprots.get(i);
			if(i==pvReprots.size()-1){
				pvrTote+=pvr.projectVisuzlize.descriptions+"("+pvr.accumulateComp+")"+pvr.projectVisuzlize.unitQuantity;
			}else{
				pvrTote+=pvr.projectVisuzlize.descriptions+"("+pvr.accumulateComp+")"+pvr.projectVisuzlize.unitQuantity+"<br/>";
			}
		}
		return pvrTote;
	}
	
	
	public static String toJSON(List<ConstructionProject> consProjects){
		if(consProjects==null){
			return "";
		}
		return new JSONSerializer().include("id", "projectName","belongDate",
				"capacity", "lineLength", "countInvest",
				"totalInvest", "itemTotal","verifyState","verifyStateStr",
				"totalAmount", "civil", "equipment", "erect", "line", "other",
						"qualityAccident", "securityAccident","remark","pvrQuanlity","pvrTotal","pvrJsonInfo")
		.exclude("*").serialize(consProjects);
	}
	
	public static List<ConstructionProject> findByDept(Dept dept){
		return ConstructionProject.find("belongDept = ? order by createDate desc", dept).fetch();
	}
	
	public static String toSimpleJSON(List<ConstructionProject> consProjects){
		return new JSONSerializer().include("consProjectId", "text")
		.exclude("*").serialize(consProjects);
	}
	/**
	 * 本部是否审核过
	 * @return true 已审核，false 未审核
	 */
	public boolean verifyByHead(){
		if(this.verifyState==-2 || this.verifyState==2){
			return true;
		}
		return false;
	}
}
